﻿using log4net;
using log4net.Layout.Pattern;
using System;
using System.Collections.Generic;
using System.Data;
using System.IO;
using System.Linq;
using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading.Tasks;
using X.LOG.log4net;
using log4netUtil = log4net.Util;

namespace X.LOG
{
    public static class Config
    {
        private static readonly object LockObject = new object();

        #region LOG4NET

        private static string _Log4NetConfigPath = null;
        internal static string Log4NetConfigPath
        {
            get
            {
                if (string.IsNullOrEmpty(_Log4NetConfigPath))
                {
                    lock (LockObject)
                    {
                        if (_Log4NetConfigPath == null)
                        {
                            var path = System.Configuration.ConfigurationManager.AppSettings["Log4NetConfigPath"];
                            if (string.IsNullOrEmpty(path))
                                throw new TypeInitializationException(typeof(Config).FullName, new Exception("初始化异常：未配置Log4NetConfigPath"));
                            if(!System.IO.File.Exists(path))
                                throw new TypeInitializationException(typeof(Config).FullName, new Exception("初始化异常：配置文件Log4NetConfigPath指定了一个不存在的路径"));
                            _Log4NetConfigPath = path;
                        }
                    }
                }
                return _Log4NetConfigPath;
            }
        }

        private static string _Log4NetRepository = null;
        internal static string Log4NetRepository
        {
            get
            {
                if(string.IsNullOrEmpty(_Log4NetRepository))
                {
                    lock (LockObject)
                    {
                        if(_Log4NetRepository == null)
                        {
                            var name = System.Configuration.ConfigurationManager.AppSettings["Log4NetRespository"];
                            if(string.IsNullOrEmpty(name))
                                throw new TypeInitializationException(typeof(Config).FullName, new Exception("初始化异常：未配置Log4NetRespository"));
                            _Log4NetRepository = name;
                        }
                    }
                }
                return _Log4NetRepository;
            }
        }
        /// <summary>
        /// 手动指定Log4Net配置文件
        /// </summary>
        /// <param name="log4NetConfigPath"></param>
        /// <exception cref="TypeInitializationException"></exception>
        public static void SetLog4NetConfigPath(string log4NetConfigPath)
        {
            if (!File.Exists(log4NetConfigPath))
                throw new TypeInitializationException(typeof(Config).FullName, new Exception("初始化异常：手动配置Log4NetPath指定了一个不存在的路径"));
            _Log4NetConfigPath = log4NetConfigPath;
        }
        /// <summary>
        /// 手动设置Log4NetRepository名称
        /// </summary>
        /// <param name="name"></param>
        /// <exception cref="TypeInitializationException"></exception>
        public static void SetLog4NetRepository(string name)
        {
            if(string.IsNullOrEmpty(name))
                throw new TypeInitializationException(typeof(Config).FullName, new Exception("初始化异常：手动配置Log4NetRepository名称不能为空"));
            _Log4NetRepository = name;
        }

        public static void SetInternalDebugging(bool enabled)
        {
            log4netUtil.LogLog.InternalDebugging = enabled;
        }


        #endregion

        #region Log4Net MDC

        public static void SetLog4NetMDC(string name, string value)
        {
            MDC.Set(name, value);
        }
        public static void GetLog4NetMDC(string name)
        {
            MDC.Get(name);
        }

        public static void RemoveLog4NetMDC(string name)
        {
            MDC.Remove(name);
        }

        public static void ClearLog4NetMDC()
        {
            MDC.Clear();
        }

        #endregion

        #region Log4Net CustomPatternConverter

        //internal static IDictionary<string, Type> CustomPatternConverters = new Dictionary<string, Type>();

        //public static void AddCustomPatternConvert(string name, Type converter)
        //{
        //    CustomPatternConverters.Add(name, converter);
        //}

        #endregion

        #region LOGSTASH
#if DEBUG
        internal const string DefaultLogStashServerIp = "192.168.0.8";
#else
        internal const string DefaultLogStashServerIp = "10.31.53.234";
#endif

        internal const int DefaultLogStashServerPort = 8080;
        #endregion
    }

 
}
